﻿using FirebirdSql.Data.FirebirdClient;
using FirebirdSql.Data.Isql;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestFirebird
{
    /// <summary>
    /// System.BadImageFormatException: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
    /// 目右键属性->项目设计器->生成->平台->把'默认设置(任何 CPU)'改为x86。
    ///因为'任何 CPU'的程序在64位的机器上就会用运行为64位，而64程序是不能加载32位dll的
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// add static method to append log to richtext box.
        /// </summary>
        public async void AppendText(string str)
        {

            await Task.Run(() =>
            {
                Console.WriteLine("");
                this.Invoke((Action)delegate
                {
                    this.richTextBox1.AppendText(str + "\r\n");
                });

            });

        }
        private void Button1_Click(object sender, EventArgs e)
        {
            var connectionString = new FbConnectionStringBuilder
            {
                Database = @"TestFile.fdb",
                ServerType = FbServerType.Embedded,
                Charset = "GB_2312",
                UserID = "tiger",

            }.ToString();
            //new FbConnectionCreateDatabase(connectionString, false);
            if (File.Exists(@"TestFile.fdb"))
            {
                AppendText("file exitst");
            }
            else
            {
                FbConnection.CreateDatabase(connectionString);
            }

            // FbConnection connection = new FbConnection("User=SYSDBA;Password=masterkey;Database=ray_linn.fdb;Charset=NONE;ServerType=1");
            FbConnection connection = new FbConnection(connectionString);

            connection.Open();
            FbTransaction transaction = connection.BeginTransaction();

            string str = @"SELECT RDB$RELATION_NAME AS TABLE_NAME
FROM RDB$RELATIONS
WHERE RDB$SYSTEM_FLAG = 0
AND RDB$VIEW_SOURCE IS NULL";
            FbCommand command = new FbCommand(str, connection, transaction);

            FbDataReader reader = command.ExecuteReader();
            reader.Read();
            while (reader.Read())
            {
                AppendText(reader.GetString(0));
            }


            transaction.Commit();

            connection.Close();
            AppendText("end");
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            AppendText("测试用户和口令");
            var connectionString = new FbConnectionStringBuilder
            {
                Database = @"TestFile.fdb",
                ServerType = FbServerType.Embedded,
                UserID = "tiger12",
                Password = "tige",

            }.ToString();
            FbConnection connection = new FbConnection(connectionString);
            connection.Open();
            FbTransaction transaction = connection.BeginTransaction();

            string str = @"SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE (RDB$FLAGS = 0 OR RDB$FLAGS IS NULL) AND RDB$VIEW_SOURCE IS NULL";
            FbCommand command = new FbCommand(str, connection, transaction);

            FbDataReader reader = command.ExecuteReader();
            reader.Read();
            while (reader.Read())
            {
                AppendText(reader.GetString(0));
            }            
            transaction.Commit();
            connection.Close();
            AppendText("测试用户和口令 结束，内嵌模式，用户名必填，口令没有作用");
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            var connectionString = new FbConnectionStringBuilder
            {
                Database = @"TestFile.fdb",
                ServerType = FbServerType.Embedded,
                UserID = "tiger",
                //Password = "tige",

            }.ToString();
            // parse the SQL script  
            FbScript script =  FbScript.LoadFromFile("script.ddl");
            script.Parse();

            //execute the SQL script  
            using (FbConnection c = new FbConnection(connectionString))
            {
                FbBatchExecution fbe = new FbBatchExecution(c);
                //foreach (FbStatement cmd in script.Results.ToArray())
                //{
                //    fbe.AppendSqlStatements.Add(cmd);
                //}
                fbe.AppendSqlStatements(script);
                fbe.Execute();
            }

            AppendText("创建表并insert 操作");
        }
    }
}
